package com.axend.lib_base.base.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.ViewDataBinding
import androidx.viewbinding.ViewBinding
import com.axend.lib_base.R
import com.axend.lib_base.base.vm.BaseViewModel
import com.axend.lib_base.databinding.BaseCommonTitlebarBinding
import com.axend.lib_base.databinding.BaseViewSearchBarBinding
import java.lang.reflect.ParameterizedType

abstract class BaseVVDFragment<VM : BaseViewModel, VB : ViewDataBinding> : BaseVMFragment<VM>() {

    protected val titleBarBinding by lazy { BaseCommonTitlebarBinding.bind(mBinding.root.findViewById(R.id.ll_top_filter)) }
    protected val searchBinding by lazy { BaseViewSearchBarBinding.bind(mBinding.root.findViewById(R.id.ll_search)) }
    private val _binding: VB by lazy(LazyThreadSafetyMode.NONE) {
        // 反射创建ViewBinding实例
        try {
            // 获取父类泛型参数的实际类型
            val clazz: Class<VB> =
                (javaClass.genericSuperclass as ParameterizedType)
                    .actualTypeArguments[1] as Class<VB>

            // 获取inflate方法并调用它来创建ViewBinding实例
            val inflateMethod = clazz.getMethod("inflate", LayoutInflater::class.java)
            val bindingInstance = inflateMethod.invoke(null, layoutInflater) as VB

            // 设置LifecycleOwner和其他必要操作
            bindingInstance.lifecycleOwner = this
            if (getBindingVariable() > 0) {
                bindingInstance.setVariable(getBindingVariable(), mViewModel)
                bindingInstance.executePendingBindings()
            }

            bindingInstance
        } catch (e: Exception) {
            e.printStackTrace()
            throw IllegalStateException("无法通过反射创建ViewBinding对象")
        }
    }
    protected val mBinding: VB
        get() = requireNotNull(_binding) { "ViewBinding对象为空" }

    protected abstract fun getBindingVariable(): Int

    // 反射创建ViewBinding
    protected open fun createViewBinding() {
//
//        try {
//            val clazz: Class<*> =
//                (this.javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[1] as Class<VB>
//            val inflateMethod = clazz.getMethod("inflate", LayoutInflater::class.java)
//            _binding = inflateMethod.invoke(null, layoutInflater) as VB
//        } catch (e: Exception) {
//            e.printStackTrace()
//            throw IllegalArgumentException("无法通过反射创建ViewBinding对象")
//        }
//        _binding?.lifecycleOwner = this
//        if (getBindingVariable() > 0) {
//            _binding!!.setVariable(getBindingVariable(), mViewModel)
//            _binding!!.executePendingBindings()
//        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }


    override fun setContentView(container: ViewGroup?): View {
        return mBinding.root
    }

    override fun getLayoutIdRes(): Int = 0

    override fun onDestroyView() {
        super.onDestroyView()
    }

}